/*
 * ioGame
 * Copyright (C) 2021 - 2023  渔民小镇 （262610965@qq.com、luoyizhu@gmail.com） . All Rights Reserved.
 * # iohao.com . 渔民小镇
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General  License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General  License for more details.
 *
 * You should have received a copy of the GNU General  License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.mygame.common.protobuf;


import com.mygame.common.io.FileKit;
import com.mygame.common.kit.StrKit;
import com.mygame.common.log.LogUtil;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * 生成 pb 文件
 *
 * @author 渔民小镇
 * @date 2022-01-25
 */

public class ProtoGenerateFile {
    /** pb 源码目录 */
    public String protoSourcePath;
    /** pb class 目录 */
    public String protoPackagePath;
    /** 生成 proto file 目录 */
    public String generateFolder;

    public static ProtoGenerateFile build(){
        ProtoGenerateFile generateFile = new ProtoGenerateFile();
        return generateFile;
    }
    private void checked() {
        Objects.requireNonNull(protoSourcePath);
        Objects.requireNonNull(protoPackagePath);
        Objects.requireNonNull(generateFolder);

        FileKit.mkdir(this.generateFolder);
    }

    public void generate() {
        checked();

        ProtoJavaAnalyse analyse = new ProtoJavaAnalyse();
        Map<ProtoJavaRegionKey, ProtoJavaRegion> regionMap = analyse.analyse(protoPackagePath, protoSourcePath);

        Consumer<ProtoJavaRegion> javaRegionConsumer = javaRegion -> {
            String fileName = javaRegion.getFileName();

            List<ProtoJava> protoJavaList = javaRegion.getProtoJavaList();

            LogUtil.info("fileName: {} - {}", fileName, protoJavaList.size());

            String protoString = javaRegion.toProtoFile();
            LogUtil.info("-------------{}---------------------", fileName);
            LogUtil.info("{}", protoString);

            String protoFilePath = StrKit.format("{}{}{}"
                    , this.generateFolder
                    , File.separator
                    , fileName
            );

            FileKit.writeUtf8String(protoString, protoFilePath);
        };

        regionMap.values().forEach(javaRegionConsumer);
    }

    public String getProtoSourcePath() {
        return protoSourcePath;
    }

    public ProtoGenerateFile setProtoSourcePath(String protoSourcePath) {
        this.protoSourcePath = protoSourcePath;
        return this;
    }

    public String getProtoPackagePath() {
        return protoPackagePath;
    }

    public ProtoGenerateFile setProtoPackagePath(String protoPackagePath) {
        this.protoPackagePath = protoPackagePath;
        return this;
    }

    public String getGenerateFolder() {
        return generateFolder;
    }

    public ProtoGenerateFile setGenerateFolder(String generateFolder) {
        this.generateFolder = generateFolder;
        return this;
    }
}
